package cn.wolfcode.web.controller;


import cn.wolfcode.domain.ClassInfo;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.ClassInfoQueryObject;
import cn.wolfcode.qo.CustomerReportQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IClassInfoService;
import cn.wolfcode.service.ICustomerReportService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;

    /**
     * 表格形式的报表
     * @param qo
     * @param model
     * @return
     */
    //查询页面信息
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")CustomerReportQueryObject qo,Model model){
        model.addAttribute("pageInfo",customerReportService.selectForList(qo));
        return "customerReport/list";
    }


    /**
     * 柱状图(不用分页)
     */
    @RequestMapping("/listBar")
    public String lisyBar(@ModelAttribute("qo") CustomerReportQueryObject qo,Model model){
        //查询数据库的分组数据
        List<HashMap> list = customerReportService.selectAll(qo);
        //先准备两个集合,分别存储x和y的数据
        List x = new ArrayList();
        List y = new ArrayList();
        //遍历sql查出来的集合list,获取到每一个map
        for (HashMap map : list) {
            //从map中获取对应的数据,分别放入x和y集合中
            x.add(map.get("groupType"));
            y.add(map.get("number"));
        }
        //共享到页面
        model.addAttribute("x",JSON.toJSONString(x));
        model.addAttribute("y",JSON.toJSONString(y));
        //共享分组类型
        model.addAttribute("groupTypeName",MessageUtil.changMsg(qo));
        return "customerReport/listBar";
    }


    /**
     * 饼图(不用分页)
     */

    @RequestMapping("/listPie")
    public String listPie(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo){
        //获取所有数据(不分页)
        List<HashMap> list = customerReportService.selectAll(qo);
        //先准备两个集合,分别存储legend和series的数据
        List legend = new ArrayList();
        List series = new ArrayList();
        //需要把数据转换为echart需要结构
        for (HashMap map : list) {
            legend.add(map.get("groupType"));
            HashMap temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("number"));
            series.add(temp); // {value: 335, name: '孙总'}
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("legend",JSON.toJSONString(legend));
        model.addAttribute("series",JSON.toJSONString(series));
        //共享分组类型
        model.addAttribute("groupTypeName",MessageUtil.changMsg(qo));
        return "customerReport/listPie";
    }
}
